from flask import Flask, jsonify, request
from config.database import db
from model.template_model import Template
from model.log_model import Log
from service.template_service import TemplateService
from dao.template_dao import TemplateDao

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)

@app.route('/templates', methods=['POST'])
def create_template():
    data = request.json
    new_template = TemplateService.createTemplate(data)
    return jsonify({'id': new_template.id}), 201

@app.route('/templates', methods=['GET'])
def get_templates():
    templates = TemplateService.getAllTemplates()
    return jsonify([t.to_dict() for t in templates])

if __name__ == '__main__':
    with app.app_context():
        db.create_all()
    app.run(debug=True)